<template>
  <div>
    <!-- Header 区域 -->
    <van-nav-bar
      fixed
      title="文章详情"
      left-arrow
      @click-left="$router.back()"
    />

    <!-- 文章信息区域 -->
    <div class="article-container">
      <!-- 文章标题 -->
      <h1 class="art-title">{{ atrticleDetallOBj.title }}</h1>

      <!-- 用户信息 -->
      <van-cell
        center
        :title="atrticleDetallOBj.aut_name"
        :label="atrticleDetallOBj.pubdate"
      >
        <template #icon>
          <img src="" alt="" class="avatar" />
        </template>
        <template #default>
          <div>
            <van-button
              type="info"
              size="mini"
              v-if="atrticleDetallOBj.is_followed"
              @click="isfollowedFn(false)"
              >已关注</van-button
            >
            <van-button
              icon="plus"
              type="info"
              size="mini"
              plain
              v-else
              @click="isfollowedFn(true)"
              >关注</van-button
            >
          </div>
        </template>
      </van-cell>

      <!-- 分割线 -->
      <van-divider></van-divider>

      <!-- 文章内容 -->
      <div class="art-content" v-html="atrticleDetallOBj.content"></div>

      <!-- 分割线 -->
      <van-divider>End</van-divider>

      <!-- 点赞 -->
      <div class="like-box">
        <van-button
          icon="good-job"
          type="danger"
          size="small"
          v-if="atrticleDetallOBj.attitude === 1"
          @click="collectedFn(false)"
          >已点赞</van-button
        >
        <van-button
          icon="good-job-o"
          type="danger"
          plain
          size="small"
          v-else
          @click="collectedFn(true)"
          >点赞</van-button
        >
      </div>
    </div>
    <!-- 评论列表 -->

    <div style="margin:50px">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <comment-list
          v-for="(obj, index) in commentsList"
          :key="index"
          :commentsObj="obj"
        ></comment-list>
            </van-list>
    </div>

    <!-- 发表评论 -->
    <div class="add-cmt-box van-hairline--top" v-if="isShowCmtInput === false" >
      <van-icon name="arrow-left" size="0.48rem" @click="$router.back()" />
      <div class="ipt-cmt-div" @click="isShowCmtInputFn(true)">发表评论</div>
      <div class="icon-box">
        <van-badge :content="commentnum">
          <van-icon name="comment-o" size="0.53333334rem" @click="lookCommentsFn()" />
        </van-badge>
        <van-icon name="star-o" size="0.53333334rem" />
        <van-icon name="share-o" size="0.53333334rem" />
      </div>
    </div>

    <!-- 底部添加评论区域 - 2 -->
    <div class="cmt-box van-hairline--top" v-else>
      <textarea
        placeholder="友善评论、理性发言、阳光心灵"
        v-fofo
        v-model="mycomment"
        @blur="isBlur"
      ></textarea>
      <van-button
        type="default"
        :disabled="mycomment.length === 0"
        @click="surecommentFn"
        >发布</van-button
      >
    </div>
  </div>
</template>

<script>
import {
  getarticlesAPI,
  delfollowAPI,
  followAPI,
  collectedAPI,
  delcollectedAPI,
  getCommentsAPI,
  myCommentAPI
} from '@/api'
import CommentList from './CommentList.vue'
export default {
  components: { CommentList },
  data () {
    return {
      atrticleDetallOBj: {},
      type: 'a',
      commentsList: [],
      isShowCmtInput: false,
      mycomment: '',
      loading: true,
      finished: false,
      offset: null,
      commentnum: ''
    }
  },
  async created () {
    const article_id = this.$route.query.aid
    const res = await getarticlesAPI(article_id)
    this.atrticleDetallOBj = res.data.data
    this.getcommentsFn()
  },
  methods: {
    onLoad () {
      this.getcommentsFn()
    },
    async getcommentsFn () {
      // 获取评论
      const res1 = await getCommentsAPI({
        type: this.type,
        source: this.$route.query.aid,
        offset: this.offset
      })
      this.commentnum = res1.data.data.total_count
      if (res1.data.data.total_count === 0) {
        this.commentnum = ''
      }
      console.log(res1)
      if (res1.data.data.results.length === 0) {
        this.finished = true
      }
      this.offset = res1.data.data.last_id
      this.commentsList = [...this.commentsList, ...res1.data.data.results]
      console.log(this.commentsList)
      this.loading = false
    },
    async isfollowedFn (bool) {
      this.atrticleDetallOBj.is_followed = !this.atrticleDetallOBj.is_followed
      if (bool === false) {
        console.log(1)
        const res = await delfollowAPI(this.atrticleDetallOBj.aut_id)
        console.log(res)
      } else {
        console.log(2)
        const res = await followAPI(this.atrticleDetallOBj.aut_id)
        console.log(res)
      }
    },
    async collectedFn (bool) {
      if (bool === false) {
        console.log(1)
        this.atrticleDetallOBj.attitude = 0
        const res = await delcollectedAPI(this.atrticleDetallOBj.art_id)

        console.log(res)
      } else {
        console.log(2)
        const res = await collectedAPI(this.atrticleDetallOBj.art_id)
        this.atrticleDetallOBj.attitude = 1

        console.log(res)
      }
    },
    async isShowCmtInputFn (bool) {
      this.isShowCmtInput = bool
    },
    isBlur () {
      setTimeout(() => {
        this.isShowCmtInput = false
      })
    },
    async surecommentFn () {
      const res1 = await myCommentAPI({
        target: this.$route.query.aid,
        content: this.mycomment
      })
      console.log(res1)
      this.mycomment = ''
      this.isShowCmtInput = false
      await getCommentsAPI({
        type: this.type,
        source: this.$route.query.aid,
        offset: this.offset
      })
      console.log(this.commentsList)
    },
    async lookCommentsFn () {
      console.log(1)
      document.querySelector('.like-box').scrollIntoView({
        behavior: 'smooth'
      })
    }
  }
}
</script>

<style scoped lang="less">
.article-container {
  padding: 10px;
  margin-top: 46px;
}
.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
  /deep/ img {
    width: 100%;
  }
  /deep/ pre {
    white-space: pre-wrap;
    word-wrap: break-word;
  }
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}
/*美化 - 文章详情 - 底部的评论页面 */
// 外层容器
.art-cmt-container-1 {
  padding-bottom: 46px;
}
.art-cmt-container-2 {
  padding-bottom: 80px;
}

// 发布评论的盒子 - 1
.add-cmt-box {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  box-sizing: border-box;
  background-color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 46px;
  line-height: 46px;
  padding-left: 10px;
  .ipt-cmt-div {
    flex: 1;
    border: 1px solid #efefef;
    border-radius: 15px;
    height: 30px;
    font-size: 12px;
    line-height: 30px;
    padding-left: 15px;
    margin-left: 10px;
    background-color: #f8f8f8;
  }
  .icon-box {
    width: 40%;
    display: flex;
    justify-content: space-evenly;
    line-height: 0;
  }
}

.child {
  width: 20px;
  height: 20px;
  background: #f2f3f5;
}

// 发布评论的盒子 - 2
.cmt-box {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 80px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  padding-left: 10px;
  box-sizing: border-box;
  background-color: white;
  textarea {
    flex: 1;
    height: 66%;
    border: 1px solid #efefef;
    background-color: #f8f8f8;
    resize: none;
    border-radius: 6px;
    padding: 5px;
  }
  .van-button {
    height: 100%;
    border: none;
  }
}
</style>
